package com.ruoyi.goods.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.goods.domain.BmCategory;
import com.ruoyi.goods.service.IBmCategoryService;
import com.ruoyi.goods.service.IBmGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/seller/category")
public class SellerBmCategoryController extends BaseController {
    @Autowired
    private IBmCategoryService bmCategoryService;
    @Autowired
    private IBmGoodsService bmGoodsService;

    /**
     * 商品一级分类
     * @return
     */
    @GetMapping("/firstList")
    public AjaxResult firstList()
    {
        List<BmCategory> list = bmCategoryService.selectBmCategoryFirstList();
        return AjaxResult.success(list);
    }

    @GetMapping("/secondList/{parentId}")
    public AjaxResult secondList(@PathVariable("parentId") Integer parentId)
    {
        List<BmCategory> list = bmCategoryService.selectBmCategorySecondListByParentId(parentId);
        return AjaxResult.success(list);
    }

    @PostMapping("/findGoodsByCategory")
    public AjaxResult findGoodsByCategory(@RequestBody Map<String,Object> map){
        List<Map<String, Object>> goodsByCategory = bmGoodsService.findGoodsByCategory(map);
        return AjaxResult.success(goodsByCategory);
    }

    @PostMapping("/addCart")
    public AjaxResult addCart(@RequestBody Map<String,Object> good){
        bmGoodsService.addCartGoods(good);
        return AjaxResult.success();
    }

    @PostMapping("/selectCartNum")
    public AjaxResult selectCartNum(@RequestBody Map<String,Object> good){
        int cartNum = bmGoodsService.selectCartNum(good);
        return AjaxResult.success(cartNum);
    }

//    @PostConstruct
    @GetMapping("/findAllGoods")
    public AjaxResult findAllGoods() throws IOException {
        List<Map<String, Object>> goods = bmGoodsService.findAllGoods();
        return AjaxResult.success(goods);
    }
    @PostMapping("/findGoodsByBnameOrGoodsName")
    public AjaxResult findGoodsByBnameOrGoodsName(@RequestBody Map<String,Object> map) throws IOException {
        List<Map<String, Object>> goods = bmGoodsService.findGoodsByBnameOrGoodsName(map);
        return AjaxResult.success(goods);
    }

}
